﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Text;

using FRL.DebtChased.Extensions;

namespace FRL.DebtChased.Repository.ViewModels
{

	/// <summary>
	/// Declaration of ClientWorkflowAssignmentsModel class
	/// </summary>
	public class ClientWorkflowAssignmentsModel
	{

		/////////////////////////////////////////////////////////////
		// Nested classes

		public class Element
		{

			/////////////////////////////////////////////////////////
			// Attributes

			internal int? m_iClientWorkflowAssignmentId = 0;
			public int? ClientWorkflowAssignmentId
			{
				get { return m_iClientWorkflowAssignmentId; }
			}

			internal int? m_iWorkflowId = 0;
			public int? WorkflowId
			{
				get { return m_iWorkflowId; }
			}
			public string m_strWorkflowAsText;
			public string WorkflowAsText
			{
				get 
				{
					if (string.IsNullOrEmpty(m_strWorkflowAsText))
						return Definitions.NotAssigned;
					else
						return m_strWorkflowAsText; 
				}
			}

			internal int m_iAccountInvoiceStatusId = 0;
			public int AccountInvoiceStatusId
			{
				get { return m_iAccountInvoiceStatusId; }
			}
			internal string m_strStatusAsText;
			public string StatusAsText
			{
				get { return m_strStatusAsText; }
			}

			internal string m_strDefaultWorkflowName;
			public string DefaultWorkflowName
			{
				get { return m_strDefaultWorkflowName; }
			}

			internal string m_strModifiedAsText;
			public string ModifiedAsText
			{
				get { return m_strModifiedAsText; }
			}

		}

		/////////////////////////////////////////////////////////////
		// Constants and Enumerations

		/////////////////////////////////////////////////////////////
		// Attributes

		internal int m_iClientId = 0;
		public int ClientId
		{
			get { return m_iClientId; }
		}

		private List<Element> m_elements = new List<Element>();
		public List<Element> Elements
		{
			get { return m_elements; }
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public ClientWorkflowAssignmentsModel( DebtChasedEntities ent, int iClientId )
		{

			// Initialize member variables
			m_iClientId = iClientId;

			// Load assignments from store procedure
			var results = ent.ClientWorkflowAssignmentsProc(m_iClientId);
			foreach (var ob in results)
			{

				// Add an element to the model
				Element el = new Element();
				el.m_iClientWorkflowAssignmentId = ob.fdClientWorkflowAssignmentId;
				el.m_iWorkflowId = ob.fdWorkflowId;
				el.m_strWorkflowAsText = ob.fdWorkflowName;
				el.m_iAccountInvoiceStatusId = ob.fdAccountInvoiceStatusId;
				el.m_strStatusAsText = ob.fdAccountInvoiceStatusName;
				el.m_strDefaultWorkflowName = ob.fdDefaultWorkflowName;
				m_elements.Add(el);

				// Modified text
				if(ob.fdModifiedAt != null)
					el.m_strModifiedAsText = string.Format("{0} by {1}",
						ob.fdModifiedAt.Value.ToCuteString(),
						ob.fdModifiedBy
					);

			} // foreach-loop

		}

	}

}
